package character.sprite.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import configuration.Paths;

import character.sprite.data.CharacterStyleDatabase;

import main.gui.ProgressPanel;

public class CharacterStyleDeploymentTool extends JDialog implements
		ActionListener {

	private static final long serialVersionUID = 2322828766753896186L;

	private JTextField dirPathText;
	private JButton browseButton, importButton;
	private ProgressPanel progress;

	public CharacterStyleDeploymentTool(CharacterSpriteBuilder parent) {
		super(parent, "Choose a plug-in directory...");

		this.setSize(400, 110);
		this.setModal(true);
		this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		this.setLocation(this.getWidth(), this.getHeight());
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLayout(new FlowLayout());

		dirPathText = new JTextField();
		dirPathText.setEditable(false);
		dirPathText.setPreferredSize(new Dimension(220, 25));
		browseButton = new JButton("Browse");
		importButton = new JButton("Import");

		browseButton.addActionListener(this);
		importButton.addActionListener(this);

		progress = new ProgressPanel();

		this.add(dirPathText);
		this.add(browseButton);
		this.add(importButton);
		this.add(progress);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String command = arg0.getActionCommand();
		if (command.equals(browseButton.getText())) {
			JFileChooser fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int result = fc.showOpenDialog(this);
			if (result == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				dirPathText.setText(file.getAbsolutePath());
			}
		} else if (command.equals(importButton.getText())) {
			if (!dirPathText.getText().equals("")) {
				new Thread(new Runnable() {
					@Override
					public void run() {
						progress.setProgress(0);
						progress.setStatus("Preparing to import plug-ins...");
						File dir = new File(dirPathText.getText());
						File[] pluginDirs = dir.listFiles();
						int count = 0;
						for (File p : pluginDirs) {
							progress.setStatus("Importing plug-in "
									+ p.getName() + "...");
							CharacterStyleDatabase.getInstance()
									.importStylePlugin(p.getAbsolutePath());
							count++;
							progress.setProgress((100 / pluginDirs.length)
									* count);
						}
						progress.setProgress(100);
						progress.setStatus("Import completed.");
					}
				}).start();
			} else {
				JOptionPane
						.showMessageDialog(
								this,
								"Choose a directory containing character/nstyle plug-ins.",
								"Choose a directory",
								JOptionPane.WARNING_MESSAGE);
			}
		}
	}

}
